package org.dromara.system.controller.system;

import lombok.RequiredArgsConstructor;
import cn.dev33.satoken.annotation.SaIgnore;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.bo.SysFaultFeedbackEnBo;
import org.dromara.system.domain.bo.SysFaultFeedbackEnQueryBo;
import org.dromara.system.domain.bo.SysFaultInfoEnQueryBo;
import org.dromara.system.domain.vo.SysFaultFeedbackEnVo;
import org.dromara.system.domain.vo.SysFaultInfoEnVo;
import org.dromara.system.service.ISysFaultFeedbackEnService;
import org.dromara.system.service.ISysFaultInfoEnService;
import org.dromara.system.service.ISysSolutionDocEnService;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import jakarta.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * Fault Query (English User Side) Controller
 * Public fault query service without permission verification
 *
 * @author system
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/user/fault-en")
@SaIgnore
public class SysFaultQueryEnController extends BaseController {

    private final ISysFaultInfoEnService faultInfoService;
    private final ISysSolutionDocEnService solutionDocService;
    private final ISysFaultFeedbackEnService faultFeedbackService;

    /**
     * Level 1 Query: Get all excavator models
     */
    @GetMapping("/models")
    public R<List<String>> getExcavatorModels() {
        List<String> models = faultInfoService.getExcavatorModelList();
        return R.ok(models);
    }

    /**
     * Level 2 Query: Get fault parts
     */
    @GetMapping("/parts")
    public R<List<String>> getFaultParts(@RequestParam(required = false) String excavatorModel) {
        List<String> parts = faultInfoService.getFaultPartList(excavatorModel);
        return R.ok(parts);
    }

    /**
     * Level 3 Query: Get fault symptoms by model and part
     */
    @GetMapping("/symptoms")
    public R<List<String>> getFaultSymptoms(@RequestParam(required = false) String excavatorModel,
                                           @RequestParam(required = false) String faultPart) {
        List<String> symptoms = faultInfoService.getFaultSymptomList(excavatorModel, faultPart);
        return R.ok(symptoms);
    }

    /**
     * Level 4 Query: Get fault details
     */
    @GetMapping("/details")
    public R<List<Map<String, Object>>> getFaultDetails(
            @RequestParam(required = false) String excavatorModel,
            @RequestParam(required = false) String faultPart,
            @RequestParam(required = false) String faultSymptom) {
        List<Map<String, Object>> details = faultInfoService.getFaultDetailList(excavatorModel, faultPart, faultSymptom);
        return R.ok(details);
    }

    /**
     * Multi-condition search
     */
    @GetMapping("/search")
    public TableDataInfo<SysFaultInfoEnVo> searchFaults(
            SysFaultInfoEnQueryBo queryBo, PageQuery pageQuery) {
        return faultInfoService.queryPageList(queryBo, pageQuery);
    }

    /**
     * Get fault information detail
     */
    @GetMapping("/{faultId}")
    public R<SysFaultInfoEnVo> getFaultInfo(@PathVariable Long faultId) {
        SysFaultInfoEnVo faultInfo = faultInfoService.queryById(faultId);
        return R.ok(faultInfo);
    }

    /**
     * Preview solution PDF
     */
    @GetMapping("/solution/preview/{faultId}")
    public void previewSolutionDoc(@PathVariable Long faultId,
                                  HttpServletResponse response) throws Exception {
        SysFaultInfoEnVo faultInfo = faultInfoService.queryById(faultId);
        if (faultInfo != null && faultInfo.getSolutionId() != null) {
            solutionDocService.previewDocument(faultInfo.getSolutionId(), "inline", response);
        } else {
            throw new RuntimeException("Fault information does not exist or no solution document");
        }
    }

    // ==================== Feedback APIs ====================

    /**
     * Submit fault feedback
     */
    @RepeatSubmit()
    @PostMapping(value = "/feedback", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<SysFaultFeedbackEnVo> submitFeedback(
        @RequestPart("feedback") @Validated(AddGroup.class) SysFaultFeedbackEnBo feedbackBo,
        @RequestPart(value = "images", required = false) MultipartFile[] images) {

        SysFaultFeedbackEnVo vo = faultFeedbackService.submitFeedback(feedbackBo, images);
        return R.ok("Submitted successfully, please wait for review", vo);
    }

    /**
     * Query my feedback list
     */
    @GetMapping("/feedback/my")
    public TableDataInfo<SysFaultFeedbackEnVo> myFeedbacks(
        SysFaultFeedbackEnQueryBo queryBo, PageQuery pageQuery) {

        try {
            queryBo.setCreateUserId(LoginHelper.getUserId());
        } catch (Exception e) {
            return TableDataInfo.build();
        }
        return faultFeedbackService.queryPageList(queryBo, pageQuery);
    }

    /**
     * Get feedback detail
     */
    @GetMapping("/feedback/{feedbackId}")
    public R<SysFaultFeedbackEnVo> getFeedbackInfo(@PathVariable Long feedbackId) {
        SysFaultFeedbackEnVo vo = faultFeedbackService.queryById(feedbackId);
        return R.ok(vo);
    }
}

